home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2000 #4 / Amiga Plus CD - 2000 - No. 4.iso / Tools / Misc / bgui / Examples / Source / TreeViewDemo.c < prev    next >
C/C++ Source or Header  |  2000-05-09  |  10KB  |  387 lines

  1. /*
  2.  * @(#) $Header: /cvsroot/bgui/gadgets/TreeView/TreeViewDemo.c,v 41.11 2000/05/09 20:36:05 mlemos Exp $
  3.  *
  4.  * BGUI Tree List View class
  5.  *
  6.  * (C) Copyright 1999 Manuel Lemos.
  7.  * (C) Copyright 1996-1999 Nick Christie.
  8.  * All Rights Reserved.
  9.  *
  10.  * $Log: TreeViewDemo.c,v $
  11.  * Revision 41.11  2000/05/09 20:36:05  mlemos
  12.  * Bumped to revision 41.11
  13.  *
  14.  * Revision 1.2  2000/05/09 20:00:59  mlemos
  15.  * Merged with the branch Manuel_Lemos_fixes.
  16.  *
  17.  * Revision 1.1.2.2  1999/05/31 01:19:23  mlemos
  18.  * Made the program create TreeView object instances using the bgui.library to
  19.  * setup the gadget library.
  20.  *
  21.  * Revision 1.1.2.1  1999/02/21 04:08:19  mlemos
  22.  * Nick Christie sources.
  23.  *
  24.  *
  25.  *
  26.  */
  27.  
  28. /************************************************************************
  29. *************************  TREEVIEW CLASS DEMO  *************************
  30. ************************************************************************/
  31.  
  32. #include <exec/exec.h>
  33. #include <dos/dos.h>
  34. #include <libraries/bgui.h>
  35. #include <libraries/bgui_macros.h>
  36.  
  37. #include <clib/alib_protos.h>
  38. #include <clib/macros.h>
  39.  
  40. #include <proto/exec.h>
  41. #include <proto/dos.h>
  42. #include <proto/intuition.h>
  43. #include <proto/bgui.h>
  44. #include <proto/utility.h>
  45.  
  46. #include <bgui/bgui_treeview.h>
  47.  
  48. /************************************************************************
  49. *****************************  DEFINITIONS  *****************************
  50. ************************************************************************/
  51.  
  52. /*
  53.  * Compiler stuff.
  54.  */
  55.  
  56. #ifdef _DCC
  57. #define SAVEDS    __geta4
  58. #define ASM
  59. #define REG(x)    __ ## x
  60. #else
  61. #ifdef __SASC
  62. #define SAVEDS    __saveds
  63. #define ASM        __asm
  64. #define REG(x)    register __ ## x
  65. #endif
  66. #endif
  67.  
  68. /*
  69.  * Object IDs
  70.  */
  71.  
  72. #define ID_TV_TEST            1
  73. #define ID_BT_ADD            2
  74. #define ID_BT_REMOVE        3
  75. #define ID_BT_QUIT            4
  76.  
  77. /*
  78.  * Some rawkey codes
  79.  */
  80.  
  81. #define RAW_RETURN        68
  82. #define RAW_UPARROW        76
  83. #define RAW_DOWNARROW    77
  84.  
  85. #define SHIFTKEYS        (IEQUALIFIER_LSHIFT|IEQUALIFIER_RSHIFT)
  86. #define CTRLKEY            (IEQUALIFIER_CONTROL)
  87. #define ALTKEYS            (IEQUALIFIER_LALT|IEQUALIFIER_RALT)
  88.  
  89. /************************************************************************
  90. *****************************  PROTOTYPES  ******************************
  91. ************************************************************************/
  92.  
  93. int main(int argc,char **argv);
  94.  
  95. ASM SAVEDS ULONG WindowHandler(REG(a0) struct Hook *hook,
  96.     REG(a2) Object *obj, REG(a1) struct IntuiMessage *imsg);
  97.  
  98. extern void __stdargs KPrintF(char *fmt,...);
  99.  
  100. /************************************************************************
  101. *****************************  LOCAL DATA  ******************************
  102. ************************************************************************/
  103.  
  104. /*
  105.  *    Library base.
  106.  */
  107.  
  108. struct Library    *BGUIBase = NULL;
  109.  
  110. /************************************************************************
  111. *******************************  MAIN()  ********************************
  112. ************************************************************************/
  113.  
  114. int main(int argc,char **argv)
  115. {
  116. if (BGUIBase = OpenLibrary("bgui.library",40))
  117.     {
  118.         struct Hook    idcmphook;
  119.         Object        *WI_Main,*TV_Test,*BT_Add,*BT_Remove,*BT_Quit;
  120.  
  121.         TV_Test = BGUI_NewObject(BGUI_TREEVIEW_GADGET,
  122.                 GA_ID,                ID_TV_TEST,
  123.                 TVA_Indentation,    12,
  124.                 TVA_ImageStyle,        TVIS_BOX,
  125.                 TVA_LineStyle,        TVLS_DOTS,
  126.                 TVA_NoLeafImage,    TRUE,
  127.                 LISTV_MinEntriesShown,5,
  128.                 PGA_NewLook,        TRUE,
  129.                 TAG_DONE);
  130.  
  131.         idcmphook.h_Entry = (HOOKFUNC) WindowHandler;
  132.         idcmphook.h_Data= (APTR) TV_Test;
  133.  
  134.         WI_Main = WindowObject,
  135.             WINDOW_Title,            "TreeView Class Demo",
  136.             WINDOW_ScreenTitle,        "TreeView Class for BGUI by Nick Christie (18.09.96)",
  137.             WINDOW_AutoAspect,        TRUE,
  138.             WINDOW_SmartRefresh,    TRUE,
  139.             WINDOW_RMBTrap,            TRUE,
  140.             WINDOW_AutoKeyLabel,    TRUE,
  141.             WINDOW_ScaleHeight,        33,
  142.             WINDOW_IDCMPHook,        &idcmphook,
  143.             WINDOW_IDCMPHookBits,    IDCMP_RAWKEY,
  144.             WINDOW_MasterGroup,
  145.                 VGroupObject, HOffset(4), VOffset(4), Spacing(4),
  146.                     StartMember,
  147.                         TV_Test,
  148.                     EndMember,
  149.                     StartMember,
  150.                         HGroupObject, HOffset(4), VOffset(4), Spacing(4),
  151.                             VarSpace(DEFAULT_WEIGHT/2),
  152.                             StartMember,
  153.                                 BT_Add = ButtonObject,
  154.                                     LAB_Label,            "_Add",
  155.                                     GA_ID,                ID_BT_ADD,
  156.                                 EndObject,
  157.                             EndMember,
  158.                             VarSpace(DEFAULT_WEIGHT/2),
  159.                             StartMember,
  160.                                 BT_Remove = ButtonObject,
  161.                                     LAB_Label,            "_Remove",
  162.                                     GA_ID,                ID_BT_REMOVE,
  163.                                     GA_Disabled,        TRUE,
  164.                                 EndObject,
  165.                             EndMember,
  166.                             VarSpace(DEFAULT_WEIGHT/2),
  167.                             StartMember,
  168.                                 BT_Quit = ButtonObject,
  169.                                     LAB_Label,            "_Quit",
  170.                                     GA_ID,                ID_BT_QUIT,
  171.                                 EndObject,
  172.                             EndMember,
  173.                             VarSpace(DEFAULT_WEIGHT/2),
  174.                         EndObject, FixMinHeight,
  175.                     EndMember,
  176.                 EndObject,
  177.             EndObject;
  178.  
  179.         if (WI_Main)
  180.             {
  181.             struct Window    *win;
  182.             BOOL            ok;
  183.  
  184.             static STRPTR    GrandParent = "Grandparent";
  185.             static STRPTR    Parent[] = {"Parent A","Parent B","Parent C"};
  186.             static STRPTR    ChildA[] = {"Child A0","Child A1","Child A2"};
  187.             static STRPTR    GrandA[] = {"Grandchild A0A","Grandchild A0B","Grandchild A2A"};
  188.             static STRPTR    ChildB[] = {"Child B0"};
  189.             static STRPTR    NewEntry = "New Entry";
  190.  
  191.             ok = (BOOL) BGUI_DoGadgetMethod(TV_Test,NULL,NULL,
  192.                 TVM_INSERT,NULL,GrandParent,TV_ROOT,TVW_CHILD_LAST,TVF_EXPAND);
  193.  
  194.             if (ok)
  195.                 ok = (BOOL) BGUI_DoGadgetMethod(TV_Test,NULL,NULL,
  196.                     TVM_INSERT,NULL,Parent[0],GrandParent,TVW_CHILD_LAST,TVF_EXPAND);
  197.  
  198.             if (ok)
  199.                 ok = (BOOL) BGUI_DoGadgetMethod(TV_Test,NULL,NULL,
  200.                     TVM_INSERT,NULL,Parent[1],GrandParent,TVW_CHILD_LAST,TVF_EXPAND);
  201.  
  202.             if (ok)
  203.                 ok = (BOOL) BGUI_DoGadgetMethod(TV_Test,NULL,NULL,
  204.                     TVM_INSERT,NULL,Parent[2],GrandParent,TVW_CHILD_LAST,TVF_EXPAND);
  205.  
  206.             if (ok)
  207.                 ok = (BOOL) BGUI_DoGadgetMethod(TV_Test,NULL,NULL,
  208.                     TVM_INSERT,NULL,ChildA[0],Parent[0],TVW_CHILD_LAST,TVF_EXPAND);
  209.  
  210.             if (ok)
  211.                 ok = (BOOL) BGUI_DoGadgetMethod(TV_Test,NULL,NULL,
  212.                     TVM_INSERT,NULL,ChildA[1],Parent[0],TVW_CHILD_LAST,TVF_EXPAND);
  213.  
  214.             if (ok)
  215.                 ok = (BOOL) BGUI_DoGadgetMethod(TV_Test,NULL,NULL,
  216.                     TVM_INSERT,NULL,ChildA[2],Parent[0],TVW_CHILD_LAST,TVF_EXPAND);
  217.  
  218.             if (ok)
  219.                 ok = (BOOL) BGUI_DoGadgetMethod(TV_Test,NULL,NULL,
  220.                     TVM_INSERT,NULL,GrandA[0],ChildA[0],TVW_CHILD_LAST,TVF_EXPAND);
  221.  
  222.             if (ok)
  223.                 ok = (BOOL) BGUI_DoGadgetMethod(TV_Test,NULL,NULL,
  224.                     TVM_INSERT,NULL,GrandA[1],ChildA[0],TVW_CHILD_LAST,TVF_EXPAND);
  225.  
  226.             if (ok)
  227.                 ok = (BOOL) BGUI_DoGadgetMethod(TV_Test,NULL,NULL,
  228.                     TVM_INSERT,NULL,GrandA[2],ChildA[2],TVW_CHILD_LAST,TVF_EXPAND);
  229.  
  230.             if (ok)
  231.                 ok = (BOOL) BGUI_DoGadgetMethod(TV_Test,NULL,NULL,
  232.                     TVM_INSERT,NULL,ChildB[0],Parent[1],TVW_CHILD_LAST,TVF_EXPAND);
  233.  
  234.             if (ok)
  235.                 {
  236.                 if (win = WindowOpen(WI_Main))
  237.                     {
  238.                     ULONG            winsig,sigs,id;
  239.                     BOOL            running;
  240.  
  241.                     GetAttr(WINDOW_SigMask,WI_Main,&winsig);
  242.                     running = TRUE;
  243.  
  244.                     while(running)
  245.                         {
  246.                         sigs = Wait(winsig|SIGBREAKF_CTRL_C);
  247.  
  248.                         if (sigs & winsig)
  249.                             {
  250.                             while((id = HandleEvent(WI_Main)) != WMHI_NOMORE)
  251.                                 {
  252.                                 switch(id)
  253.                                     {
  254.                                     case WMHI_CLOSEWINDOW:
  255.                                     case ID_BT_QUIT:
  256.                                         running = FALSE;
  257.                                         break;
  258.  
  259.                                     case ID_BT_ADD:
  260.                                         if (BGUI_DoGadgetMethod(TV_Test,win,NULL,TVM_INSERT,
  261.                                             NULL,NewEntry,TV_SELECTED,TVW_CHILD_FIRST,0))
  262.                                             {
  263.                                             SetGadgetAttrs((struct Gadget *) BT_Add,
  264.                                                 win,NULL,GA_Disabled,TRUE,TAG_DONE);
  265.                                             SetGadgetAttrs((struct Gadget *) BT_Remove,
  266.                                                 win,NULL,GA_Disabled,FALSE,TAG_DONE);
  267.                                             }
  268.                                         break;
  269.  
  270.                                     case ID_BT_REMOVE:
  271.                                         if (BGUI_DoGadgetMethod(TV_Test,win,NULL,TVM_REMOVE,
  272.                                             NULL,NewEntry,TVW_ENTRY,0))
  273.                                             {
  274.                                             SetGadgetAttrs((struct Gadget *) BT_Add,
  275.                                                 win,NULL,GA_Disabled,FALSE,TAG_DONE);
  276.                                             SetGadgetAttrs((struct Gadget *) BT_Remove,
  277.                                                 win,NULL,GA_Disabled,TRUE,TAG_DONE);
  278.                                             }
  279.                                         break;
  280.  
  281.                                     default:
  282.                                         break;
  283.                                     }
  284.                                 }
  285.                             }
  286.  
  287.                         if (sigs & SIGBREAKF_CTRL_C)
  288.                             running = FALSE;    
  289.                         }
  290.                     }
  291.                 }
  292.  
  293.             DisposeObject(WI_Main);
  294.         }
  295.     CloseLibrary(BGUIBase);
  296.     }
  297. return(0);
  298. }
  299.  
  300. /************************************************************************
  301. ***************************  WINDOWHANDLER()  ***************************
  302. *************************************************************************
  303. * Hook for main window IDCMP messages. Called by window object after its
  304. * intuimsg processing has finished, before main() gets control. Used
  305. * here to provide extra keyboard control not possible using normal
  306. * gadget-key assignment, specifically: crsr up/down to select entry in
  307. * treeview and ESC to close window.
  308. *
  309. *************************************************************************/
  310.  
  311. ASM SAVEDS ULONG WindowHandler(REG(a0) struct Hook *hook,
  312.     REG(a2) Object *obj, REG(a1) struct IntuiMessage *imsg)
  313. {
  314. struct Window    *win;
  315. ULONG            cursel,method,entry,which,flags;
  316. Object            *view;
  317.  
  318. view = (Object *) hook->h_Data;
  319. win = imsg->IDCMPWindow;
  320.  
  321. if (imsg->Class == IDCMP_RAWKEY)
  322.     {
  323.     method = TVM_SELECT;
  324.     entry = (ULONG) TV_SELECTED;
  325.     which = ~0;
  326.     flags = TVF_MAKEVISIBLE;
  327.     cursel = DoMethod(view,TVM_GETENTRY,TV_SELECTED,TVW_ENTRY,0);
  328.  
  329.     if (imsg->Code == RAW_UPARROW)
  330.         {
  331.         if (cursel)
  332.             {
  333.             which = TVW_TREE_PREV;
  334.  
  335.             if (imsg->Qualifier & SHIFTKEYS)
  336.                 which = TVW_TREE_PAGE_UP;
  337.  
  338.             if (imsg->Qualifier & CTRLKEY)
  339.                 {
  340.                 entry = (ULONG) TV_ROOT;
  341.                 which = TVW_CHILD_FIRST;
  342.                 }
  343.             }
  344.         else
  345.             {
  346.             which = TVW_CHILD_FIRST;
  347.             entry = (ULONG) TV_ROOT;
  348.             }
  349.         }
  350.  
  351.     if (imsg->Code == RAW_DOWNARROW)
  352.         {
  353.         if (cursel)
  354.             {
  355.             which = TVW_TREE_NEXT;
  356.  
  357.             if (imsg->Qualifier & SHIFTKEYS)
  358.                 which = TVW_TREE_PAGE_DOWN;
  359.  
  360.             if (imsg->Qualifier & CTRLKEY)
  361.                 {
  362.                 entry = (ULONG) TV_ROOT;
  363.                 which = TVW_TREE_LAST;
  364.                 }
  365.             }
  366.         else
  367.             {
  368.             which = TVW_CHILD_FIRST;
  369.             entry = (ULONG) TV_ROOT;
  370.             }
  371.         }
  372.  
  373.     if (imsg->Code == RAW_RETURN)
  374.         {
  375.         method = TVM_EXPAND;
  376.         which = TVW_ENTRY;
  377.         flags = TVF_TOGGLE;
  378.         }
  379.  
  380.     if (which != ~0)
  381.         BGUI_DoGadgetMethod(view,win,NULL,method,
  382.             NULL,entry,which,flags);
  383.     }
  384.  
  385. return(0);
  386. }
  387.